import QtQuick 1.0
//import ShellInvoker 1.0
import "../../../common/lib/kdeadmouseutils-build-desktop"

Item {
    /*Keys.onEscapePressed : {
       appControl.hide();
     }
     
    Keys.onBackPressed : {
       console.log("tonBackPressed");
       event.accepted = true;
       appControl.hide();
    }*/
     
    id: mainItem
    clip:false
    property string appHome : "/socMDC/KdeadMouse/apps/youtube"
    property int indexPlaying : -1
    property bool hideable : false
    anchors.centerIn: parent
    height : 700
    width : 1024
    
    
    
    Timer {
	 function startTimer () {
	    stop();
	    if(!mainItem.hideable){
	      console.log("timer disabled");
	      return;
	    }
	    console.log("timer enabled===> start");
	    start();
	 }
	 id:hideTimer
         interval: 5000
         running: false
         repeat: false
         onTriggered: {
	   //hide
	   appControl.hide();
	 }
     }

    DbusService {
	id : videoGene
	service:'org.kdeadmouse.apps.videoplayer'
	path:'/generic/videoplayer'
	registerName : 'gene_videoplayer'
	Component.onCompleted: {
	  checkServiceExists();
	}
    }
    
    DbusService {
	id:serv
	
	signal started()
	signal allFinished(string videoType)
	signal keyEvent(int key,string videoType)
	
	service:'org.kdeadmouse.apps.videoplayer'
	path:'/specific/videoplayer'
	registerName : 'videoplayer'
	
	onStarted : {
	    console.log('strated');
	    mainItem.hideable=true;
	    
	}
	
	onKeyEvent : {
	  console.log('keyEvent type:' + videoType); 
	  if(videoType!='youtube'){
	      return;
	  }
	  console.log('keyEvent:' + key);
	  appControl.show();
	}
	
	onAllFinished : {
	    mainItem.hideable=false;
	    if(videoType!='youtube'){
	      return;
	    }
	    console.log('===============tentative suivant===================>finish:');
	    var videoId = playingListLoader.item.getModelItem(mainItem.indexPlaying+1).vId;
	    console.log('next:'+videoId);
	    mainItem.indexPlaying=mainItem.indexPlaying+1;
	    serv.invokeDbusMethod('loadNowVideo',['youtube',videoId]);
	    playingListLoader.item.currentVId=videoId;
	  }
	
	
	onInitialized :{
	   console.log('serv completed');
	   videoplayer.keyEvent.connect(keyEvent);
	   videoplayer.started.connect(started);
	   videoplayer.allFinished.connect(allFinished);
	   console.log('connect 2');
	}
	
	Component.onCompleted: {
	  checkServiceExists();
	}
    }
    
    Item {
	id:homeItem
	width: 500
	height: 700
	anchors.verticalCenter : parent.verticalCenter
	Loader {
	    id:homeLoader
	    source: "home.qml"
	    //focus: true //is set to true for the Loader as well as the Item in the dynamically loaded 
	    onLoaded : {
	      console.log('home loaded');
	    }
	}
	Connections {
	    target: homeLoader.item
	    onSelect : {
	      console.log('pL selected:' + id);
	      playingListLoader.item.plId=id;
	      mainItem.state='playingListState';
	    }
	 }
    }
    
   
    Item {
	id:loadingListItem
	width: 1024
	height: 400
	anchors.bottom: parent.bottom
	Loader {
	    id: playingListLoader
	    source: "listeVideo.qml"
	    //focus : true
	    /*onLoaded : {
	      console.log('loadingListItem loaded');
	    }*/
	}
	
	
	
	Connections {
	    target: playingListLoader.item
	    onSelectVideo : {
	      console.log('video selected:' + videoId+' ,index:'+index);	      
	      mainItem.indexPlaying=index;
	      serv.invokeDbusMethod('loadNowVideo',['youtube',videoId]);
	      playingListLoader.item.currentVId=videoId;
	      console.log('####################### show');
	      videoGene.invokeDbusMethod('show',[]);
	    }
	 }
    }
    
    
    
    /*
    Item {
	id:relatedListItem
	width: 1024
	height: 400
	anchors.bottom: parent.bottom
	anchors.left : parent.right
	Loader {
	    source: "listeVideo.qml"
	    focus:true
	    onLoaded : {
	      console.log('loadingListItem loaded');
	    }
	}
    }
    */
    
  	
    Keys.onLeftPressed : {
	  if(mainItem.state == 'playingListState'){
	    mainItem.state='homeState';
	    event.accepted = true;
	    console.log('=========>homeState');
	    return;
	  }
    }
    
    Keys.onRightPressed : {
	if(mainItem.state == 'homeState'){
	    mainItem.state='playingListState';
	    event.accepted = true;
	    return;
	}
    }
    
   Keys.onPressed: {
	 hideTimer.startTimer();
         if (event.key == Qt.Key_Backspace) {
             console.log("Key_Backspace");
             event.accepted = true;
	     appControl.hide();
         }
   }
   Keys.onEscapePressed : {
      appControl.hide();
      event.accepted = true;
   }
    //states
    
    states: [
	 
	 
         
         State {
             name: "homeState"
	     AnchorChanges	{ target: loadingListItem; anchors.right : parent.left }
	     AnchorChanges	{ target: homeItem; anchors.horizontalCenter : parent.horizontalCenter}
	     
	     
	     //focus
	     PropertyChanges	{ target: homeLoader;focus: true }
         },
	 
         State {
             name: "playingListState"
	     AnchorChanges	{ target: homeItem; anchors.left : parent.right }
	     AnchorChanges	{ target: loadingListItem; anchors.left : parent.left }
	     PropertyChanges	{ target: playingListLoader;focus: true }
         }
         
         //vide
         /*State {
             name: "vide"
	     AnchorChanges	{ target: loadingListItem; anchors.right : parent.left }
	     AnchorChanges	{ target: homeItem; anchors.bottom : parent.top }
	     
	     //focus (en attandant le controle des videos)
	     PropertyChanges	{ target: homeLoader;focus: false }
	     PropertyChanges	{ target: playingListLoader;focus: false }
         }*/
         
         
         
    ]
    
    transitions: Transition {
         AnchorAnimation { duration: 1000 ; easing.type:Easing.OutInQuad }
    }
    
    Component.onCompleted: {
      mainItem.state='homeState';
      //homeItem.focus = true
 
    }
    
}
